/* $Header: mux.h,v 2.2 01/10/02 10:30:32 przybyls Exp $ */

/***********************************************************************/
/* INCLUDE FILE NAME: mux.h                                            */
/* ==================                                                  */
/*                                                                     */
/* Author: Lesley Freed                                                */
/*                                                                     */
/* COMMENT: This include file contains all of the flag bit def'ns      */
/*          used in the servicing of the multiplexed ADC.              */
/*                                                                     */
/*                                                                     */
/*                                                                     */
/***********************************************************************/

#ifndef MUX_H
#define MUX_H

/*#include <typedefs.h> */

/*****************************/
/*   IFDEFS                  */
/*****************************/


/*****************************/
/*   SYMBOLIC CONSTANTS      */
/*****************************/

/* Mux'ed adc Constants */

/* Define the adc state table end of table flags */

#define ZONE_MAX_TICK_COUNT 4

/* Define the adc state table end of table flags */

#define CONTINUE_SUBTABLE   0
#define MUX_END_OF_SUBTABLE 1
#define MUX_END_OF_TABLE    2

/* Zone mux adc state table entries */

   /* Define # of readings to reading consecutively */
#define OVEN_RDG_COUNT 4
#define ZONE_RDG_COUNT 1

/* Define the mux adc test limits */

#define NOMINAL_OFFSET  32769                   /*  500.023 mV*2^16 counts/V */
#define NOMINAL_OFFSET2 62181                   /*  948.812 mV*2^16 counts/V */
#define NOMINAL_DELTA   (NOMINAL_OFFSET2-NOMINAL_OFFSET)

#define OFFSET_ERR      500                     /*  offset error ~= .5% of FS */
                                                /*   = .005*(2^16)            */
                                                /*   : (.1% for resistor      */
                                                /*   + .1% for adc) times 2   */
                                                /*   + gain amplifier offsets */
                                                /*     (200)                  */


#define GAIN_ERR        500                     /*  gain error ~= .5% of FS   */
                                                /*  offset (offset2-offset)   */
                                                /*   : (.1% for resistor      */
                                                /*   + .05% for adc) times 2  */
                                                /*   + gain amplifier offsets */
                                                /*     (200)                  */

#define MIN_OFFSET      (-1*OFFSET_ERR)
#define MAX_OFFSET      (OFFSET_ERR)
#define MIN_GAIN        (NOMINAL_DELTA-GAIN_ERR)
#define MAX_GAIN        (NOMINAL_DELTA+GAIN_ERR)

/* Define the line sense reading test limits */

#define NOMINAL_LINE    40238   /*        0.614 V * 2^16 = 100 % of nom  */
#define MIN_LINE        16095   /*  0.4 * 0.614 V * 2^16 =  40 % of nom  */
#define MAX_LINE        52310   /*  1.3 * 0.614 V * 2^16 = 130 % of nom  */

/* Maximum # of Additional Bits of Resolution caused by summing readings  */
/* NOTE: by adding 64 readings you would get log2(64) or 6 add'l bits of  */
/*       resolution                                                       */

#define MAX_SHIFT       0x06

/* Mux Adc Hardware */

#define BUSY_FLAG 0x2   /* Bit position for Adc BUSY flag in PORTF */

/***************************/
/* Mux Adc Channel Selects */
/***************************/

#define GENERIC_MUX_ADC_ADDR 0xff  /* FOR debug:  this is to test adc */

#define OVEN_TEMP_ADDR   0x14
#define DETA_TEMP_ADDR   0x12
#define DETB_TEMP_ADDR   0x11
#define INJA_TEMP_ADDR   0x16
#define INJB_TEMP_ADDR   0x17
#define AUXA_TEMP_ADDR   0x13
#define AUXB_TEMP_ADDR   0x10
#define ADC_OFFSET_ADDR  0x0E

#define LINE_SENSE_ADDR  0x0C
/* #define PNEU_24V_ADDR   0x45 */

 #define DET_DIAGB_ADDR  0x09
 #define DET_DIAGC_ADDR  0x0A
 #define DET_DIAGA_ADDR  0x08
 #define DET_DIAGD_ADDR  0x0B

/* #define ATTN1_ADDR      0x40 */
/* #define ATTN2_ADDR      0x41 */

/* #define DAC1_ADDR       0x42 */
/* #define DAC2_ADDR       0x43 */

#define GAIN_OFFSET_ADDR 0x15
/* #define MON_24V_ADDR    0x47 */

/* #define MON_5V_ADDR     0x60 */
/* #define MON_15VPOS_ADDR 0x61 */
/* #define MON_15VNEG_ADDR 0x62 */
/* #define MON_GND_ADDR    0x63 */

#define SPARE1_ADDR      0x0D
#define SPARE2_ADDR      0x0F

/* Flag bits for Thermal Reading */

#define GAIN_OFFSET_ID  0x00001
#define PNEU_24V_ID     0x00002
#define SPARE1_ID       0x00040
#define SPARE2_ID       0x00080
#define OVEN_TEMP_ID    0x00800

#define DETA_TEMP_ID    0x01000
#define DETB_TEMP_ID    0x02000
#define INJA_TEMP_ID    0x04000
#define INJB_TEMP_ID    0x08000
#define AUXA_TEMP_ID    0x10000
#define AUXB_TEMP_ID    0x20000

#define LINE_SENSE_ID   0x40000

#define ADC_OFFSET_ID   0x80000

/* Diagnostic Channel Flags */

#define DET_DIAGA_ID    0x100000
#define DET_DIAGB_ID    0x200000
#define DET_DIAGC_ID    0x400000
#define DET_DIAGD_ID    0x800000

#define ATTN1_ID        0x1000000
#define ATTN2_ID        0x2000000

#define DAC1_ID         0x4000000
#define DAC2_ID         0x8000000

#define MON_GND_ID      0x02
#define MON_5V_ID       0x10000000
#define MON_24V_ID      0x20000000
#define MON_15VPOS_ID   0x40000000
#define MON_15VNEG_ID   0x80000000

/***************************/
/*   MACROS                */
/***************************/


/*****************************/
/*   TYPEDEFS                */
/*****************************/

typedef volatile U_INT8  MUX_ADDR;

typedef struct
{
        volatile U_INT8         chan_addr;
        volatile U_INT8         next_chan_addr;
                 U_INT16        *chan_rdg;
                 BIT32          chan_id;
} MUX_REC, *pMUX_REC;

/*****************************/
/*   EXTERNALS               */
/*****************************/

extern  BIT8      Mux_Rdg_Addr;
extern  BIT8      Generic_Mux_Rdg_Addr;
extern  BIT8      Pre_Channel_Rdg_Addr;
extern  BIT8      Pre_Channel_Flag;

/*****************************/
/*   FUNCTION PROTOTYPES     */
/*****************************/

        /* mux_adc.c functions */
        void    InitializeMuxAdc(void);
        U_INT32 ReadMuxAdc( MUX_ADDR mux_addr, U_INT8 count );
        void    MonitorMuxAdcNoise( void );
        BIT8    StartMuxAdcTimeResponseTest( BIT8 from );
        void    EndMuxAdcTimeResponseTest( void );
        void    CalcMuxAdcOffset( void );
        void    EvalMuxAdcOffset( void );

#pragma SECTION PROG=SysCode
        void    MuxAdc( void );
        void    MuxAdcStartup( void );
        void    MuxAdcTRTest( void );
#pragma SECTION UNDO

#endif
